$(function () {
   /*动态响应轮播图*/
   banner();
   initTab();
});

var banner = function () {
    var $banner = $(".wjs_banner");
    var $point = $banner.find(".carousel-indicators");
    var $imgBox = $banner.find(".carousel-inner");
    var $window = $(window);

    var data = [
        {
            pcSrc:"images/slide_01_2000x410.jpg",
            mSrc:"images/slide_01_640x340.jpg"
        },
        {
            pcSrc:"images/slide_02_2000x410.jpg",
            mSrc:"images/slide_02_640x340.jpg"
        },
        {
            pcSrc:"images/slide_03_2000x410.jpg",
            mSrc:"images/slide_03_640x340.jpg"
        },
        {
            pcSrc:"images/slide_04_2000x410.jpg",
            mSrc:"images/slide_04_640x340.jpg"
        }
    ];

    /*渲染*/
    var render = function () {
        var isM = $window.width()<768?true:false;
        var pointHtml = "";
        var imgHtml = "";
        $.each(data,function (i,item) {
            pointHtml += '<li data-target="#carousel-example-generic" data-slide-to="'+ i +'" '+ (i==0?'class="active"':'') +'></li>\n';
            imgHtml += '<div class="item '+ (i==0?"active":"") +'">';
            if(isM){
                imgHtml += '<a href="#" class="m_imgBox hidden-lg hidden-md hidden-sm"><img src="'+ item.mSrc +'" alt=""></a>';
            }else{
                imgHtml += '<a href="#" class="pc_imgBox hidden-xs" style="background-image: url('+ item.pcSrc +')"></a>';
            }
            imgHtml += '</div>';
        });

        $point.html(pointHtml);
        $imgBox.html(imgHtml);
    };

    //让屏幕每次改变大小都渲染一次
    $window.on("resize",function () {
        render();
    }).trigger("resize");

    //  $window.trigger("resize");
    //  主动触发window resize事件，使图片初始化

    // 添加滑动事件
    var starX = 0;
    var distanceX = 0;
    var flag = false;
    $banner.on("touchstart",function (e) {
        starX = e.originalEvent.touches[0].clientX;
    }).on("touchmove",function (e) {
        distanceX = e.originalEvent.touches[0].clientX - starX;
        flag = true;
    }).on("touchend",function (e) {
        if(flag && Math.abs(distanceX)>=50){
            if(distanceX>0){
                $banner.carousel('prev');
            }else{
                $banner.carousel('next');
            }
        }

        starX = 0;
        distanceX = 0;
        flag = false;
    })

};

var initTab = function () {
    /*
    1.将ul宽度设置成可以放入所有li标签宽度
    2.套一个大盒子
    3.为大盒子绑定iScroll事件
     */
    var tab = $(".wjs_product .nav-tabs");
    var liList = tab.find("li");
    var width = 0;
    $.each(liList,function (i,item) {
        width += $(item).outerWidth(true);
    });
    tab.width(width);

    new IScroll('.nav-tabs-parent',{
        scrollX:true,
        scrollY:false
    });
};